package server;


//import static org.junit.Assert.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import server.ClientController;
import server.Message;
import junit.framework.TestCase;

public class ServerTest{
	
	/**
	 * Make sure Server is running. 
	 * 
	 */

	static ClientController driver1;
	static ClientController driver2;
	private Socket socket;
	private PrintWriter out;
	private BufferedReader in;
	private Socket socket2;
	private PrintWriter out2;
	private BufferedReader in2;

	

	@Before public void setup() throws IOException {
		
		socket = new Socket("localhost", 4444);
		out = new PrintWriter(socket.getOutputStream(), true);
		in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		driver1 = new ClientController(socket, in, out);
		Thread thread1 = new Thread(driver1);
		thread1.start();
		System.out.println("socket1 started");
		
		socket2 = new Socket("localhost", 4444);
		out2 = new PrintWriter(socket2.getOutputStream(), true);
		in2 = new BufferedReader(new InputStreamReader(socket2.getInputStream()));
		driver2 = new ClientController(socket2, in2, out2);
		Thread thread2 = new Thread(driver2);
		thread2.start();
		System.out.println("socket2 start");
		
		//logging users in
		assertCorrectResponse1(Message.loginRequest("haitao"), Message.loginSuccessful("haitao"));
		assertCorrectResponse2(Message.loginRequest("victor"), Message.loginSuccessful("victor"));
	
	}


	@After public void teardown() throws IOException {
		System.out.println("logging users out...");
		//assertCorrectResponse1(Message.logOutRequest("haitao"), Message.logOutSuccessful());
		//assertCorrectResponse2(Message.logOutRequest("victor"), Message.logOutSuccessful());
		
/*		System.out.println("closing drivers");
		if (driver1 != null) {
			driver1.close();
		}
		driver1 = null;
		if (driver2 != null) {
			driver2.close();
		}
		driver2 = null;*/
		
	}
	

	
	/**
	 * Tests logging in and shows that a user must be registered before they
	 * can log into the system.
	 * Tests chat message reception and shows that you cant message a user
	 * that is not online.
	 * @throws IOException
	 */
	@Test public void testChatMessaging() throws IOException {
		
		
		// chat message reception
		System.out.println("begin test chat message");
		assertCorrectResponse1(Message.chatMessage("haitao", "hello world"),
								Message.chatMessage("haitao", "hello world"));
		//System.out.println("chatting");
		//assertCorrectResponse2(Message.chatMessage("victor", "yoyoyo"),
								//Message.chatMessage("victor", "yoyoyo"));
		

					
	}


	private void assertCorrectResponse1(String input, String response) throws IOException {
		
		driver1.writeLine(input);
		System.out.println("input: " + input );
		String line = driver1.readLine();
		System.out.println("assert equal: " + " response " + response + " line: " + line);
		Assert.assertEquals(response, line);
		
/*		String line1 = driver1.readLine();
		
		String line2 = driver1.readLine();
		if (line2 == null)
		{
			System.out.println("line 1 is " + line1);
			Assert.assertEquals(response, line1);
			return;
		}
		
		System.out.println("line 2 is " + line2);
		Assert.assertEquals(response, line2);
*/	}

	private void assertCorrectResponse2(String input, String response) throws IOException {
		driver2.writeLine(input);
		
		
		Assert.assertEquals(response, driver2.readLine());
		
/*		
 * String line1 = driver2.readLine();
 * 		String line2 = driver2.readLine();
		if (line2 == null)
		{
			System.out.println("line1 is " + line1 + " line 2 is " + line2);
			Assert.assertEquals(response, line1);
			return;
		}
		System.out.println("line 2 is " + line2);
		Assert.assertEquals(response, line2);	*/	
	}
	
	
}
